using Lucene.Net.Documents;
using Lucene.Net.Index;

namespace Webshop20.AntiCorruptionLayers.Lucene
{
    public class IndexerFacade : LuceneFacadeBase, IIndexerFacade
    {

        private IndexWriter indexWriter;

        public IndexerFacade()
        {
            indexWriter = new IndexWriter(dir, analyzer);
        }

        public void AddDocumentToIndex(Document document)
        {
            indexWriter.AddDocument(document);
            indexWriter.Optimize();
            indexWriter.Close();
        }

        public void RemoveDocumentFromFacade(Document document)
        {
            var indexReader = IndexReader.Open(dir);
            var term = new Term("Id", document.GetField("Id").StringValue());
            indexReader.DeleteDocuments(term);
        }
    }
}